package com.lapelife.backend.controller;

import com.lapelife.backend.common.ResponseResult;
import com.lapelife.backend.model.domain.SysUser;
import com.lapelife.backend.model.request.UserRequest;
import com.lapelife.backend.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/person")
@Api(tags = "用户个人信息管理")
public class UserController {

    private SysUserService sysUserService;

    @Autowired
    public void setSysUserService(SysUserService sysUserService) {
        this.sysUserService = sysUserService;
    }

    @ApiOperation("查看个人信息")
    @GetMapping("/info")
    @PreAuthorize("hasAuthority('user:person:info')")
    public ResponseResult<SysUser> userInfo() {
        return sysUserService.info();
    }

    @ApiOperation("修改个人信息")
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('user:person:update')")
    public ResponseResult<String> userUpdateInfo(@RequestBody UserRequest user) {
        return sysUserService.updateUserInfo(user);
    }

    @ApiOperation("忘记密码")
    @PostMapping("/pass/code")
    public ResponseResult<String> userUpdatePassByCode(@RequestBody UserRequest user) {
        return sysUserService.updatePasswordByCode(user);
    }

    @ApiOperation("修改账号密码")
    @PostMapping("/pass")
    @PreAuthorize("hasAuthority('user:person:pass')")
    public ResponseResult<String> userUpdatePass(@RequestBody UserRequest user) {
        return sysUserService.updatePassword(user);
    }

    @ApiOperation("发送验证码")
    @PostMapping("/code")
    public ResponseResult<String> sendCode(@RequestBody UserRequest userRequest) {
        return sysUserService.sendCode(userRequest.getEmail());
    }
}
